package summer.string;

public class Solution151 {
    public String reverseWords(String s) {
        s = s.trim();
        StringBuffer sb = new StringBuffer();
        int i = s.length() - 1;
        int j = i;
        while (i >= 0) {
            while (j >= 0 && s.charAt(j) != ' ') j--;
            sb.append(s, j + 1, i + 1).append(' ');
            while (j >= 0 && s.charAt(j) == ' ') j--;
            i = j;
        }
        return sb.toString().trim();
    }
}
